package dd.prog2.exercises.set13.mvc_gui;

import java.util.Observable;

import dd.prog2.exercises.set13.game.AI_player;

//import dd.prog2.exercises.set13.game.Game;

public class Model_c4 extends Observable {

	public AI_player ai;

	public int[][] buttons = new int[42][2];//[i][0] - player index; [i][1] - enabled flag
	private int board_size = 42;
	private int player = 1;
	private int turnCounter = 0;
	public boolean gameOver = false;
	public int winner = 0;
	
	public Model_c4() {
		// TODO Auto-generated constructor stub
		ai = new AI_player(this);
	}

	/*
	 * get coin at position i
	 */
	int getCoinAt(int i){
		return buttons[i][0];
	}
	
	public void setNextTurn(int index){
		boolean freeCellFound = false;
//		System.out.println("setting next turn");
		do{
//			System.out.println("searching for index "+index);
			if(buttons[index][1] == 0)
				index += 7;
			else {
				freeCellFound = true;
//				System.out.println("index found "+index);
				break;
			}
		} while(index < 42);
		if(freeCellFound){
			if (index - 7 >= 0) //enable next(row) button
				this.buttons[index - 7][1] = 1;
			buttons[index][0] = player;
			if (player == 1) {
				buttons[index][0] = 1;
				//.setBackground(Color.RED)
			} else{
				buttons[index][0] = 2;
				//.setBackground(Color.YELLOW);
			}
			buttons[index][1] = 0;
			turnCounter++;
			if (player == 1){
				player = 2;
			}else{
				player = 1;
			}
//			System.out.println("changing player to "+player);
			if (turnCounter > 4)
				isGameOver();
		}
		//view update
		setChanged();
		notifyObservers();
			// return "Success"
	}
	
	private void isGameOver() {
		// turnCounter=42? => draw
		if (turnCounter == 42) {
			winner = 0;
			gameOver = true;
		}
		// check winner horiz
//		System.out.println("horiz search:");
		if (!gameOver) {
			for (int i = 0; i < 6; i++) {
				for (int j = 0; j < 4; j++) {
					if (buttons[i*7+j][0] != 0)
						if (buttons[i*7+j][0] == buttons[i*7+j + 1][0]
								&& buttons[i*7+j][0] == buttons[i*7+j + 2][0]
								&& buttons[i*7+j][0] == buttons[i*7+j + 3][0]) 
						{
							winner = buttons[i*7+j][0];
							gameOver = true;
						}
				}
			}
		}
		// check winner vertical
//		System.out.println("vertical search:");
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 7; j++) {
					if (buttons[i*7+j][0] != 0)
						if (buttons[i*7+j][0] == buttons[(i+1)*7+j][0]
								&& buttons[i*7+j][0] == buttons[(i+2)*7+j][0]
								&& buttons[i*7+j][0] == buttons[(i+3)*7+j][0]) {
							winner = buttons[i*7+j][0];
							gameOver = true;
						}
				}
			}
		}
		// check winner diag right
//		System.out.println("diag right search:");
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 4; j++) {
					if (buttons[i*7+j][0] != 0)
						if (buttons[i*7+j][0] == buttons[(i+1)*7+j + 1][0]
								&& buttons[i*7+j][0] == buttons[(i+2)*7+j + 2][0]
								&& buttons[i*7+j][0] == buttons[(i+3)*7+j + 3][0]) {
							winner = buttons[i*7+j][0];
							gameOver = true;
						}
				}
			}
		}
		// check winner diag left
//		System.out.println("diag left search:");
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 3; j < 7; j++) {
					if (buttons[i*7+j][0] != 0) {
						if (buttons[i*7+j][0] == buttons[(i+1)*7+j - 1][0]
								&& buttons[i*7+j][0] == buttons[(i+2)*7+j - 2][0]
								&& buttons[i*7+j][0] == buttons[(i+3)*7+j - 3][0]) {
							winner = buttons[i*7+j][0];
							gameOver = true;
						}

					}
				}
			}
		}
	}
	
	public void reset(){
		for (int i = 0; i < board_size; i++) {
			buttons[i][0] = 0; // remove coins
			player = 1;
			turnCounter = 0;
			gameOver = false;
			winner = 0;
			buttons[i][1] = 0; // "disable all buttons"
			if(i>34)
				buttons[i][1] = 1; // except bottom row
		}
		//view update
		setChanged();
		notifyObservers();
	}
	
	// getter
	public int getPlayer() {
		return player;
	}

	public int getWinner() {
		return winner;
	}
}
